<%@ page import="com.actimind.actitime.beans.UserBean,
                 com.actimind.actitime.Keys,
                 com.actimind.actitime.util.TimeUtils,
                 com.actimind.actitime.resources.TaskResource,
                 com.actimind.actitime.resources.TaskListSortConstants,
                 com.actimind.actitime.task.ViewTaskDetailsAction"%><%@ page errorPage="/error.jsp" %>
<%@ taglib uri="/WEB-INF/at.tld" prefix="at" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
    <title><bean:write name="title-prefix" scope="application"/> Open Tasks</title>

    <script language="JavaScript" src="../js/at.js"></script>
    <script language="JavaScript" src="../js/calendar.js?r=1"></script>
    <script language="JavaScript">

    function selectCompletionDate(dateStr)
    {
        selectDateFields(dateStr, document.forms[1], 'completionDate');
    }

    function getCompletionDate()
    {
        return getDateStr(document.forms[1], 'completionDate');
    }


    function selectDateFields(dateStr, frm, fieldPrefix)
    {
        date = strToDate(dateStr);

        var yearFld = frm[fieldPrefix + 'Year'];

        var yearIndex = date.getFullYear() - yearFld.options[0].value;

        if (yearIndex >= 0 && yearIndex < yearFld.options.length)
        {
            yearFld.options[yearIndex].selected = true;
        }

        frm[fieldPrefix + 'Month'].options[date.getMonth()].selected = true;
        frm[fieldPrefix + 'Day'].options[date.getDate() - 1].selected = true;
    }

    function getDateStr(frm, fieldPrefix)
    {
        var year    = frm[fieldPrefix + 'Year'].options[frm[fieldPrefix + 'Year'].selectedIndex].value;
        var month   = frm[fieldPrefix + 'Month'].options[frm[fieldPrefix + 'Month'].selectedIndex].value;
        var day     = frm[fieldPrefix + 'Day'].options[frm[fieldPrefix + 'Day'].selectedIndex].value;

        return  year + '-' + month + '-' + day;
    }

    function checkTaskIsSelected(form)
    {
        var els = form.elements;
        var taskSelected = false;
        var re = new RegExp("taskId[[0-9]+]");
        for (var i = 0; i < els.length; i++)
        {
            if (els[i].name != null && els[i].name != "" && re.test(els[i].name))
            {
                if (els[i].checked == true)
                    taskSelected = true;
            }
            if (taskSelected)
                break;
        }
        return taskSelected;
    }

    function sortList(sortBy)
    {
        var frm = document.forms[0];
        var fields = frm .elements;

        if (fields['sortBy'].value == sortBy)
            fields['asc'].value = fields['asc'].value == 'true' ? 'false' : 'true';
        else
            fields['asc'].value = 'true';

        fields['sortBy'].value = sortBy;

        frm.submit();
    }

    function popupCalendar(date, query)
    {
       var url = '<html:rewrite page="/calendar.do" />?';
       var cWnd = window.open(url + query + '&dateStr=' + date, 'popup', 'width=223,height=320,scrollbars=no,status=no,resizable=no,top=200,left=200');
       cWnd.opener = self;
       cWnd.focus();
    }

    </script>

    <link rel="stylesheet" href="../css/at.css" type="text/css">
    <link rel="stylesheet" href="../css/calendar.css" type="text/css">
</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">

<jsp:include page="../top.jsp" flush="false">
    <jsp:param name="pageSection" value="tasks" />
    <jsp:param name="pageAction" value="otasklist" />
</jsp:include>

<table cellspacing="0" cellpadding="5" align="center" valign="top" border="0" bgcolor="#ffffff" width="770">

<bean:define id="pager" name="<%=Keys.PAGER%>" scope="session" type="com.actimind.actitime.util.Pager" />
<bean:define id="form" name="OpenedTaskListForm" scope="session" type="com.actimind.actitime.task.OpenedTaskListForm" />
<bean:define id="tasksList" name="form" property="tasksContainer" type="com.actimind.actitime.task.TaskList" />

<at:form scope="session" action="/tasks/otasklist.do" method="post" >

<input type="hidden" name="submitted" value="1" />

<html:hidden property="sortBy" />
<html:hidden property="asc" />

<logic:greaterThan value="0" property="size" name="tasksList">
    <input type="hidden" name="showRecord" value="0" />
</logic:greaterThan>

<html:hidden property="recordsPerPage" />

<tr>
    <td class="pagetitle">
        &nbsp;Open Tasks
    </td>
    <td align="right">
    <logic:equal name="form" property="ableToManageTasks" value="true">
        <input type="button" value="Add New Task" onclick="javascript:document.location.href='<html:rewrite page="/tasks/taskadd.do"/>'" />
    </logic:equal>
    </td>
</tr>
<tr>
    <td colspan="2">
        <table cellspacing="0" cellpadding="10" align="center" valign="top" border="0" width="100%">
        <tr>
            <td nowrap="1" class="formbuttonpane" style="text-align:left; vertical-align:middle">
                <table cellspacing="0" cellpadding="0" align="left" valign="top" border="0" width="100%">
                <tr>
                    <td class="text">
                        Customer:&nbsp;
                    </td>
                    <td class="text">
                        <at:select property="customerProject.shownCustomer" childSelect="customerProject.shownProject" firstChildText="-- All projects --" firstChildValue="0" style="width: 350px" maxlength="57">
                            <at:option value="0">-- All customers --</at:option><at:options collection="<%=Keys.CUSTOMERS_LIST_KEY%>" property="id" labelProperty="name" />
                        </at:select>&nbsp;
                    </td>
                    <td class="text">
                        Project:&nbsp;
                    </td>
                    <td class="text">
                        <at:select property="customerProject.shownProject" parentSelect="customerProject.shownCustomer" firstChildText="-- All projects --" firstChildValue="0" style="width: 250px" maxlength="38">
                            <at:options collection="<%=Keys.PROJECTS_LIST_KEY%>" property="id" parentProperty="customerId" labelProperty="name" />
                        </at:select>&nbsp;
                    </td>
                </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class="formbuttonpane" style="text-align:left">
                <html:submit value="    Show Tasks    " />
            </td>
        </tr>
        </table>
    </td>
</tr>
<tr>
    <td colspan="2" class="pagetitle">
        <at:errors />
        <at:messages id="message" message="true">
            <span class="successmsg"><bean:write name="message"/></span>
        </at:messages>
    </td>
</tr>
<tr>
    <td colspan="2" class="text">
    &nbsp;<b>Filter criteria:</b>
    customer:&nbsp;<bean:write name="form" property="shownCustomerName"/>;
    project:&nbsp;<bean:write name="form" property="shownProjectName"/>
    </td>
</tr>

</at:form>

<html:form scope="session" action="/tasks/otaskcomplete.do" method="post" >
<input type="hidden" name="submitted" value="1" />
<tr>
    <td colspan="2">
        <table border="0" cellspacing="1" cellpadding="5"  align="center" width="770">
        <tr>
            <td width="15%"></td>
            <td width="15%"></td>
        <logic:equal name="form" property="ableToManageTasks" value="true">
            <td width="32%"></td>
            <td width="14%"></td>
            <td width="14%"></td>
            <td width="10%"></td>
        </logic:equal>
        <logic:equal name="form" property="ableToManageTasks" value="false">
            <td width="42%"></td>
            <td width="14%"></td>
            <td width="14%"></td>
        </logic:equal>
        </tr>
        <tr>
            <th nowrap=1 class="listtblcolheader" align="center">
                <a class="listtblcolheader" href="javascript:sortList(<%=TaskListSortConstants.ORDER_TASKS_BY_CUSTOMER_PROJECT_TASK%>);">Customer</a>
                <logic:equal name="form" property="sortBy" value="<%=(new Integer(TaskListSortConstants.ORDER_TASKS_BY_CUSTOMER_PROJECT_TASK)).toString()%>">
                    <logic:equal name="form" property="asc" value="true">
                        <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                    <logic:equal name="form" property="asc" value="false">
                        <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                </logic:equal>
                <html:img page="/img/pixel.gif" width="1" height="50" align="absmiddle"/>
            </th>
            <th nowrap=1 class="listtblcolheader" align="center">
                <a class="listtblcolheader" href="javascript:sortList(<%=TaskListSortConstants.ORDER_TASKS_BY_PROJECT_CUSTOMER_TASK%>);">Project</a>
                <logic:equal name="form" property="sortBy" value="<%=(new Integer(TaskListSortConstants.ORDER_TASKS_BY_PROJECT_CUSTOMER_TASK)).toString()%>">
                    <logic:equal name="form" property="asc" value="true">
                        <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                    <logic:equal name="form" property="asc" value="false">
                        <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                </logic:equal>
            </th>
            <th nowrap=1 class="listtblcolheader" align="center">
                <a class="listtblcolheader" href="javascript:sortList(<%=TaskListSortConstants.ORDER_TASKS_BY_TASK_CUSTOMER_PROJECT%>);">Task</a>
                <logic:equal name="form" property="sortBy" value="<%=(new Integer(TaskListSortConstants.ORDER_TASKS_BY_TASK_CUSTOMER_PROJECT)).toString()%>">
                    <logic:equal name="form" property="asc" value="true">
                        <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                    <logic:equal name="form" property="asc" value="false">
                        <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                </logic:equal>
            </th>
            <th nowrap=1 class="listtblcolheader" align="center">
                <a class="listtblcolheader" href="javascript:sortList(<%=TaskListSortConstants.ORDER_TASKS_BY_DEADLINE%>);">Deadline</a>
                <logic:equal name="form" property="sortBy" value="<%=(new Integer(TaskListSortConstants.ORDER_TASKS_BY_DEADLINE)).toString()%>">
                    <logic:equal name="form" property="asc" value="true">
                        <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                    <logic:equal name="form" property="asc" value="false">
                        <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                </logic:equal>
            </th>
            <th nowrap=1 class="listtblcolheader" align="center">
                <a class="listtblcolheader" href="javascript:sortList(<%=TaskListSortConstants.ORDER_TASKS_BY_LASTTT_TASK_CUSTOMER_PROJECT%>);">Last<br>Time-Track<br>Date</a>
                <logic:equal name="form" property="sortBy" value="<%=(new Integer(TaskListSortConstants.ORDER_TASKS_BY_LASTTT_TASK_CUSTOMER_PROJECT)).toString()%>">
                    <logic:equal name="form" property="asc" value="true">
                        <img src="../img/tr_up.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                    <logic:equal name="form" property="asc" value="false">
                        <img src="../img/tr_bot.gif" width="11" height="11" border="0" hspace="0" vspace="0"/>
                    </logic:equal>
                </logic:equal>
            </th>
        <logic:equal name="form" property="ableToManageTasks" value="true">
            <th class="listtblcolheader" align="center">Select Tasks to Complete<br><nobr><a href="javascript:setCheckboxesState(document.forms[1], 'taskId', true)" class="listtblcolheadersmall"><img src="../img/box_checked.gif" border="0" hspace="0" vspace="0" height="10" width="10" alt="Checked box"/>All</a>&nbsp;<a href="javascript:setCheckboxesState(document.forms[1], 'taskId', false)" class="listtblcolheadersmall"><img src="../img/box_unchecked.gif" border="0" hspace="0" vspace="0" height="10" width="10" alt="Unchecked box"/>None</a></nobr></th>
        </logic:equal>
        </tr>

        <logic:iterate id="curTask" name="tasksList" property="tasks" type="com.actimind.actitime.beans.TaskBean">
            <bean:define id="curTaskProject" name="curTask" property="project" type="com.actimind.actitime.beans.ProjectBean" />
            <bean:define id="curTaskCustomer" name="curTaskProject" property="customer" type="com.actimind.actitime.beans.CustomerBean" />

            <tr>
                <td class="listtblcell"><bean:write name="curTaskCustomer" property="name" /></td>
                <td class="listtblcell"><bean:write name="curTaskProject" property="name" /></td>
                <td class="listtblcell"><html:link href='<%="task_details.do?taskId=" + curTask.getId() + "&origin=" + ViewTaskDetailsAction.OPEN_TASKS_ORIGIN%>'><bean:write name="curTask" property="name" /></html:link></td>
                <td class="listtblcell" nowrap="1" align="center"><bean:write name="curTask" property="printableDeadlineDate" /></td>
                <td class="listtblcell" nowrap="1" align="center"><bean:write name="curTask" property="printableLastTTDate" /></td>
            <logic:equal name="form" property="ableToManageTasks" value="true">
                <td class="listtblcell" align="center">
                <% if("N/A".equals(curTask.getPrintableLastTTDate())) { %>
                      <html:link href='<%="otaskdelete.do?taskId=" + curTask.getId()%>' onclick="return confirm('Are you sure you want to delete this task?');">delete</html:link>
                <% }  else { %>
                <bean:define id="lastTaskProject" name="curTask" property="project" type="com.actimind.actitime.beans.ProjectBean" />
                <input type="checkbox" name="taskId[<bean:write name="curTask" property="id" />]" value="1" />
                <% } %>
                </td>
            </logic:equal>
            </tr>
        </logic:iterate>
        </table>
    </td>
</tr>

<tr>
    <td colspan="2">
        <table border="0" cellspacing="0" cellpadding="0"  align="center" width="770">
        <tr>
        <td class="text" with="20%">Results contain <b><bean:write name="tasksList" property="originalSize" /></b>
            <logic:greaterThan value="1" name="tasksList" property="originalSize">tasks</logic:greaterThan>
            <logic:equal value="1" name="tasksList" property="originalSize">task</logic:equal>
        </td>
        <%  if (pager.getCurrentPages().length > 1) {%>
            <td class="text" align="center">
                <logic:notEmpty name="pager" property="previousPageFirstRecordNumber">
                <a href="javascript:document.forms[0].elements['showRecord'].value='<%=pager.getPreviousPageFirstRecordNumber().intValue()%>';document.forms[0].submit();">
                Previous
                </a>
                </logic:notEmpty>
                <logic:empty name="pager" property="previousPageFirstRecordNumber">
                Previous
                </logic:empty>
                --&nbsp;&nbsp;&nbsp;
                <logic:iterate name="pager" property="currentPages" id="curPage">
                    <logic:equal value="true" name="curPage" property="current">
                        <bean:write name="curPage"/>
                    </logic:equal>
                    <logic:notEqual value="true" name="curPage" property="current">
                        <a href="javascript:document.forms[0].elements['showRecord'].value='<%=((com.actimind.actitime.util.Pager.Page)curPage).getFirstRecordNumber()%>';document.forms[0].submit();"><bean:write name="curPage"/></a>
                    </logic:notEqual>
                    &nbsp;
                </logic:iterate>
                &nbsp;&nbsp;
                --
                <logic:notEmpty name="pager" property="nextPageFirstRecordNumber">
                <a href="javascript:document.forms[0].elements['showRecord'].value='<%=pager.getNextPageFirstRecordNumber().intValue()%>';document.forms[0].submit();">
                Next
                </a>
                </logic:notEmpty>
                <logic:empty name="pager" property="nextPageFirstRecordNumber">
                Next
                </logic:empty>
            </td>
        <%  } //if %>
        <logic:greaterThan value="0" property="size" name="tasksList">
            <td class="text" align="right">Show&nbsp;&nbsp;
                <html:select name="pager" property="recordsPerPage"
                        onchange="javascript:document.forms[0].elements['recordsPerPage'].value = this.options[this.selectedIndex].value; document.forms[0].submit();" >
                    <html:options name="pager" property="recordsPerPageCollection"/>
                </html:select>&nbsp;&nbsp;&nbsp;tasks on a page
                <script>sel = document.forms[1].elements['recordsPerPage']; for (i = 0; i < sel.options.length; i++) if (sel.options[i].value == document.forms[0].elements['recordsPerPage']) {sel.selectedIndex = i; break;};</script>
            </td>
        </logic:greaterThan>
        </tr>
        </table>
    </td>
</tr>
<logic:equal name="form" property="ableToManageTasks" value="true">
<logic:greaterThan value="0" property="size" name="tasksList">
<logic:present name="lastTaskProject">
<tr>
    <td colspan="2" align="center">
        <table cellspacing="0" cellpadding="10" align="center" valign="top" border="0" width="100%">
        <tr>
            <td class="formbuttonpane">
                <span class="text">
                <logic:messagesPresent property="completionDate"><span class="errormsg">Select date to complete tasks on:</span></logic:messagesPresent>
                <logic:messagesNotPresent property="completionDate"><b>Select date to complete tasks on:</b></logic:messagesNotPresent>
                </span>
            </td>
            <td class="formbuttonpane" style="text-align: left">
                <span class="text">
                <html:radio property="useLastTTDate" value="true" />&nbsp;Last time track date (individually for each task)<br />
                <html:radio property="useLastTTDate" value="false" />
                <html:select property="completionDateMonth">
                    <html:option value="1">January</html:option>
                    <html:option value="2">February</html:option>
                    <html:option value="3">March</html:option>
                    <html:option value="4">April</html:option>
                    <html:option value="5">May</html:option>
                    <html:option value="6">June</html:option>
                    <html:option value="7">July</html:option>
                    <html:option value="8">August</html:option>
                    <html:option value="9">September</html:option>
                    <html:option value="10">October</html:option>
                    <html:option value="11">November</html:option>
                    <html:option value="12">December</html:option>
                </html:select>&nbsp;
                <html:select property="completionDateDay">
                <%
                    for (int day = 1; day <= 31; day++)
                    {
                %><html:option value="<%=(new Integer(day)).toString()%>"><%=(day < 10 ? "0" : "")%><%=day%></html:option>
                <%
                    }
                %>
                </html:select>&nbsp;
                <%
                    int yearFrom = TimeUtils.getStartYearForDropDown();
                    int yearTo = TimeUtils.getEndYearForDropDown(0);
                %>
                <html:select property="completionDateYear">
                <%
                    for (int year = yearFrom; year <= yearTo; year++)
                    {
                %><html:option value="<%=(new Integer(year)).toString()%>"><%=year%></html:option>
                <%
                    }
                %>
                </html:select>
                  <a href="javascript: popupCalendar(getCompletionDate(), 'yearFrom=<%=yearFrom%>&yearTo=<%=yearTo%>&selectDate=selectCompletionDate')"><img src="../img/cal.gif" width="20" height="20" border="0" hspace="0" vspace="0" align="top" alt="Click Here to Pick up the timestamp"></a>
                </span>
            </td>
            <td class="formbuttonpane">
                <input type="submit" value=" Complete Selected Tasks " onClick="if (checkTaskIsSelected(document.forms[1]) == true) return confirm('Are you sure you want to complete selected tasks?'); else {alert('Please select task(s) to complete'); return false;}"/>
            </td>
        </tr>
        </table>
    </td>
</tr>
</logic:present>
</logic:greaterThan>
</logic:equal>
</html:form>
</table>

<p>

</body>
</html>
